package network;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;


public class HttpURL {

	URL url;
	HttpURLConnection connection 	=	null; 
	public String headerName		=	null; 
	public String sessionCookie		=	null;
		
	public String get( String url ) throws Exception 
    {
    	String body_html="";
    	
    	try{
    		    	
	    	URL endereco = new URL( url );    	
	    	
	        URLConnection conn = endereco.openConnection();      
	        
	        //if( this.sessionCookie != null)
	        //	this.connection.setRequestProperty("Cookie", this.sessionCookie); 
	        
	        conn.addRequestProperty("User-Agent", 
	        	    				"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0) Gecko/20100101 Firefox/8.0");
	
	        BufferedReader in = new BufferedReader(
	                                new InputStreamReader(
	                                conn.getInputStream()));
	        String inputLine;
	
	        while ((inputLine = in.readLine()) != null){
	            //System.out.println(inputLine);
	        	body_html=body_html+inputLine;
	        }
	        in.close();
    	
    	}catch( Exception e ){
    		
    		throw new Exception("404 Not Found \n"+e.getMessage());

    	}
    	
        return body_html;
    }
	
	
	public String post(String targetURL, String urlParameters)
    {
      //URL url;
      //HttpURLConnection connection = null;
      
      
      try {
        //Create connection
        this.url = new URL(targetURL);
        
        this.connection = (HttpURLConnection) this.url.openConnection();
        
        if( this.sessionCookie != null)
        	this.connection.setRequestProperty("Cookie", this.sessionCookie);  
        
        this.connection.setRequestMethod("POST");
        
        this.connection.addRequestProperty("User-Agent", 
											"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0) Gecko/20100101 Firefox/8.0");
        
        this.connection.setRequestProperty("Content-Type", 
        									"application/x-www-form-urlencoded");
  			
        this.connection.setRequestProperty("Content-Length", "" + 
                 							Integer.toString(urlParameters.getBytes().length));
        
        this.connection.setRequestProperty("Content-Language", "en-US");  
  			
        //this.connection.setUseCaches (false);
        this.connection.setDoInput(true);
        this.connection.setDoOutput(true);

        
        //Send request
        DataOutputStream wr = new DataOutputStream (
                    					this.connection.getOutputStream ());
        
        wr.writeBytes ( urlParameters );
        wr.flush ();
        wr.close ();
        

        //Get Response	
        InputStream is = this.connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        
        String line;
        StringBuffer response = new StringBuffer();
        
        while((line = rd.readLine()) != null) {
          response.append(line);
          response.append('\r');
        }
        
        rd.close();
        
        
        //cookies
         
        if( this.sessionCookie ==null ){
        	       
	        for (int i=1; (this.headerName = this.connection.getHeaderFieldKey(i))!=null; i++) {  
	        	
	        	if (this.headerName.equalsIgnoreCase("Set-Cookie")) {                     
	        		if(this.sessionCookie == "") {  
	        			this.sessionCookie = this.connection.getHeaderField(i);  
	        		}  
	        		else {  
	        			this.sessionCookie += "; " + this.connection.getHeaderField(i);  
	        		}  
	        	}  
	        }
        }
        
        return response.toString();
        

      } catch (Exception e) {

        e.printStackTrace();
        return null;

      } finally {

        if(this.connection != null) {
          this.connection.disconnect(); 
        }
      }
    }
	
}
